package com.wiegand.loader {
	import com.wiegand.error.ErrorHandler;
	import com.wiegand.event.LoaderEvent;

	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.errors.IllegalOperationError;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;

	/**
	 * @author kwiegand
	 */
	 
	public class SwfLoader extends Sprite {

		private static var _instance : SwfLoader;
		
		private var _file : String;
		private var _loader : Loader;
		private var _loading : Boolean = false;
		
		public static function getInstance() : SwfLoader{
			
			if( _instance == null )
				_instance = new SwfLoader( new SingletonEnforecer() );
			
			return _instance;
			
		}
		
		public function SwfLoader( blocker:SingletonEnforecer ):void{
			
			if ( blocker == null ) {
				
            	throw new Error("Error: Instantiation failed: Use SwfLoader.getInstance() instead of new.");
          
          	}
          	else{
          	
          		init();
          	
          	}
		
		}

		private function init():void{
		
			_loader = new Loader();	
			_loader.contentLoaderInfo.addEventListener( ErrorEvent.ERROR, errorHandler );
			_loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, errorHandler );	
			
		
		}
		
		public function load( file : String ):void{
		
		
			trace("load file: "+file);
			_file = file;
		
			_loading = true;
			
			dispatchEvent( new LoaderEvent( LoaderEvent.EVENT_START_LOADING ) );

			_loader.contentLoaderInfo.addEventListener( Event.INIT, initListener );			
			_loader.load( new URLRequest( _file ) );	
		}
		
		
		private function errorHandler( e: ErrorEvent) :void{
				
			ErrorHandler.getInstance().show(2);
			
		}
		
		private function initListener( e : Event ) : void {
			
			_loading = false;

			dispatchEvent( new LoaderEvent( LoaderEvent.EVENT_LOADING_COMPLETE ) );
			
		}
		
		public function getClass( className: String ): Class{
		      
			try{
		    	
		    	return _loader.contentLoaderInfo.applicationDomain.getDefinition( className ) as Class;
		   
		    }
		    
			catch( e: Error ){
				
		    	throw new IllegalOperationError( "ERROR: " + className + " not found!" ) ;
		   		ErrorHandler.getInstance().show(2);
		   	}
		        
		    return null;
		        
		}
		
		
		public function initialized():Boolean{
			if(_loader== null){
				return false;
			}
			else{
				if(_loader.content!=null){
					return true;
				}
				else{
					return false;
				}
			}
		}
		
		public function getDisplayObject( className: String ): DisplayObject{
		      		      
			try{
		    	
		    	var c : Class = getClass( className );
		    	
		    	var o : DisplayObject = new c();
		    	
		    	return o;	    	
		    	
		    }
		    
			catch( e: Error ){
				
		    	throw new IllegalOperationError( "ERROR: " + DisplayObject + " ---> " + className + " can't build!!!" ) ;
		         ErrorHandler.getInstance().show(2);
		   	}
		        
		    return null;
		        
		}
		
		public function clear():void{
		
			_loader = null;
		
		}
					
	}	
}

internal class SingletonEnforecer {}
